#include<bits/stdc++.h>
using namespace std;
const int N = 100001;

int n;
int ls[N],rs[N];
struct Node{
    int val,id;
    Node(){}
    Node(int val_,int id_):val{val_},id{id_}{}
    bool operator<(const Node& node)const{
        return this->val<node.val;
    }
};
int vals[N];
Node nodes[N];
int sta[N],top=1;
int root;
void print(int id){
    if(id==0) return;
    printf("%d ",vals[id]);
    print(ls[id]);
    print(rs[id]);
}

int main(){
    scanf("%d",&n);
    memset(ls,0,sizeof(ls));
    memset(rs,0,sizeof(rs));
    for(int i=1;i<=n;i++){
        scanf("%d",&nodes[i].val);
        vals[i] = nodes[i].val;
        nodes[i].id = i;
    }
    sort(nodes+1,nodes+n+1);
    sta[top] = 1;
    for(int i=2;i<=n;i++)
    {
        int c_id = nodes[i].id;
        while(top && c_id<nodes[sta[top]].id){
            top--;
        }
        if(!top){
            ls[c_id] = nodes[sta[1]].id;
        }
        else{
            ls[c_id] = rs[ nodes[sta[top]].id ];
            rs[ nodes[sta[top]].id ] = c_id;
        }
        sta[++top] = i;
    }
    print(1);
    return 0;
}